package cn.ydt.ydt.community.webapi.service.impl;

import cn.ydt.ydt.common.exception.YdtServiceException;
import cn.ydt.ydt.common.resful.StatusCode;
import cn.ydt.ydt.community.service.IOutService;
import cn.ydt.ydt.community.webapi.mapper.IOutMapper;
import cn.ydt.ydt.community.webapi.utils.SecurityContextUtils;
import cn.ydt.ydt.pojo.community.dto.AddOutDTO;
import cn.ydt.ydt.pojo.community.model.OutRequest;
import cn.ydt.ydt.pojo.community.vo.OutPeopleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class OutServiceImpl implements IOutService {
    @Autowired
    IOutMapper iOutMapper;

    @Override
    public void addOutApply(AddOutDTO addOutDTO) {
        log.info("判断传入数据是否为空");
        if (addOutDTO == null){
            log.warn("输入数据为空,抛出异常");
            throw new YdtServiceException(StatusCode.BAD_REQUEST, "输入数据为空!");
        }
        log.info("判断用户是否外出");
        Long userId = addOutDTO.getOutId();
        OutPeopleVO outPeopleVO = iOutMapper.selectOutPeopleIsCome(userId);
        if (outPeopleVO != null){
            log.warn("用户外出,抛出异常");
            throw new YdtServiceException(StatusCode.BAD_REQUEST, "用户处于外出状态,无法再次申请外出!");
        }

        log.info("创建一个outrequest对象");
        OutRequest outRequest = new OutRequest();
        BeanUtils.copyProperties(addOutDTO,outRequest);
        LocalDateTime dateTime = LocalDateTime.now();
        outRequest.setUserId(1L);
        outRequest.setIsCheck((byte)0);
        outRequest.setIsCome((byte)0);
        outRequest.setIsDelete((byte)0);
        outRequest.setCreateTime(dateTime);
        outRequest.setModifyTime(dateTime);
        log.info("userid到后面解析加入");
        Long id = SecurityContextUtils.getUserId();
        outRequest.setUserId(id);
        log.info("符合外出条件,外出信息插入数据表");
        iOutMapper.addOutApply(outRequest);
        log.info("外出申请插入成功");
    }

    @Override
    public List<OutPeopleVO> selectPersonalOut() {
        Long userId = SecurityContextUtils.getUserId();
        List<OutPeopleVO> personalOutRecord = iOutMapper.selectPersonalOut(userId);
        return personalOutRecord;
    }

    @Override
    public List<OutPeopleVO> selectOutRequestIsCheck() {
        List<OutPeopleVO> outPeopleRequest = iOutMapper.selectOutRequestIsCheck();
        return outPeopleRequest;
    }

    @Override
    public List<OutPeopleVO> selectOutPeopleList() {
        List<OutPeopleVO> outPeopleList = iOutMapper.selectOutPeopleList();
        return outPeopleList;
    }

    @Override
    public int updateIsCheck(Long userId) {
        return 0;
    }

    @Override
    public int updateIsComeForOut(Long userId) {
        return 0;
    }

    @Override
    public int updateIsComeForBack(Long userId) {
        return 0;
    }

    @Override
    public List<OutRequest> getIsCheckList() {
        List<OutRequest> list = iOutMapper.getIsCheckList();
        return list;
    }

    @Override
    public void IsCheckTrue(Long outId) {
        if (outId == null){
            throw new YdtServiceException(StatusCode.BAD_REQUEST,"该外出申请不存在");
        }
        iOutMapper.IsCheckTrue(outId);
    }

    @Override
    public void IsCheckFalse(Long outId) {
        if (outId == null){
            throw new YdtServiceException(StatusCode.BAD_REQUEST,"该外出申请不存在");
        }
        iOutMapper.IsCheckFalse(outId);
    }

}
